package com.situ.fruit;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.JDBC.JDBCUtil;

public class RecordDao {
	/**
	 * 执行insert-sql插入一条记录
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public Integer insert(RecordModel model) {// add save 面向对象的开发
		String sql = "insert into f_record(name,number,cost,time)values(?,?,?,?)";
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement ps = null;
		if (conn == null)
			return null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, model.getName());
			ps.setInt(2, model.getNumber());
			ps.setFloat(3, model.getCost());
			ps.setString(4, model.getTime());
			return ps.executeUpdate(); // 执行sql语句，并返回影响的行数
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据单个查询条件 - 执行查询sql，返回结果构成的实体类集合
	 * 
	 */
	public List<RecordModel> selectListOfTime(String start, String end) {
		// 要进行字符串拼接，使用StringBuffer/StringBuilder
		String sql = "select id,name,number,cost,time from f_record where time between '" + start + "' and '" + end
				+ "'";
		Connection conn = JDBCUtil.getConnection();
		if (conn == null)
			return null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<RecordModel> list = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			RecordModel r = null;
			while (rs.next()) {// ORM（把数据库的表的信息转换成数据库的过程）
				r = new RecordModel();
				r.setId(rs.getInt("id"));
				r.setName(rs.getString("name"));
				r.setNumber(rs.getInt("number"));
				r.setCost(rs.getFloat("cost"));
				r.setTime(rs.getString("time"));
				list.add(r);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	/**
	 * 根据多个查询条件 - 执行查询sql，返回结果构成的实体类集合
	 * 
	 */
	public List<RecordModel> selectList(RecordModel model) {
		// 要进行字符串拼接，使用StringBuffer/StringBuilder
		StringBuffer sql = new StringBuffer("select id,name,number,cost,time from f_record ");
		List<Object> vals = appendWhere(sql, model);
		Connection conn = JDBCUtil.getConnection();
		if (conn == null)
			return null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<RecordModel> list = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++)
				ps.setObject(i + 1, vals.get(i));
			rs = ps.executeQuery();
			RecordModel r = null;
			while (rs.next()) {// ORM（把数据库的表的信息转换成数据库的过程）
				r = new RecordModel();
				r.setId(rs.getInt("id"));
				r.setName(rs.getString("name"));
				r.setNumber(rs.getInt("number"));
				r.setCost(rs.getFloat("cost"));
				r.setTime(rs.getString("time"));
				list.add(r);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	private List<Object> appendWhere(StringBuffer sql, RecordModel model) {
		sql.append(" where 1=1 ");
		List<Object> vals = new ArrayList<>();
		String name = model.getName();
		if (name != null && !name.trim().isEmpty()) { // !code.trim().isEmpty()：不是空字符串（即空格一类的）
			sql.append(" and name = ? ");
			vals.add(name.trim());
		}
		Integer number = model.getNumber();
		if (number != null) {
			sql.append(" and number = ? ");
			vals.add(number);
		}
		String time = model.getTime();
		if (time != null) {
			sql.append(" and time = ? ");
			vals.add(time);
		}

		return vals;
	}
}
